using AutoMapper;
using Blog.Repository;
using Blog.Repository.Role;
using Blog.Service.Role.Input;
using Blog.Service.Role.Output;
using System;
using System.Linq;
using System.Threading.Tasks;
using XjjXmm.Infrastructure.Repository;
using XjjXmm.Infrastructure.Repository.Model;
using XjjXmm.Infrastructure.User;

namespace Blog.Service.Role
{
    public class RoleService : CrudService<RoleModel, RoleEntity>, IRoleService
    {
        private readonly IRoleRepository _roleRepository;
       // private readonly IAdRolePermissionRepository _rolePermissionRepository;

        public RoleService(IRoleRepository roleRepository, IUser user, IMapper mapper) : base(roleRepository, user, mapper)
        {
            _roleRepository = roleRepository;
        }

        //public RoleService(
        //    IRoleRepository roleRepository,
        //    IRepositoryBase<RolePermissionEntity> rolePermissionRepository
        //)
        //{
        //    _roleRepository = roleRepository;
        //    _rolePermissionRepository = rolePermissionRepository;
        //}

       

        public async Task<PageOutput<RoleListOutput>> Page(PageInput<RoleListInput> input)
        {
            //var key = input.Filter?.Name;

            //var list = await _roleRepository.Select
            //.WhereIf(key.NotNull(), a => a.Name.Contains(key))
            //.Count(out var total)
            //.OrderByDescending(true, c => c.Id)
            //.Page(input.CurrentPage, input.PageSize)
            //.ToListAsync<RoleListOutput>();

            //var data = new PageOutput<RoleListOutput>()
            //{
            //    List = list,
            //    Total = total
            //};

            //return data;
           
            var result = await _roleRepository.Queryable().ToPage(input.CurrentPage,input.PageSize);

            var dto = _mapper.Map<PageOutput<RoleListOutput>>(result);

            return dto;
            //return new PageOutput<RoleListOutput>
            //{
            //    CurrentPage = input.CurrentPage,
            //    Total = result.Total,
            //    PageSize = input.PageSize,
            //    Data = dto
            //};
        }

       
    }
}